import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGMinePurchaseOrderCell extends StatefulWidget {
  dynamic data;
  QGMinePurchaseOrderCell({super.key, this.data});
  @override
  State<QGMinePurchaseOrderCell> createState() =>
      QGMinePurchaseOrderCellState();
}

class QGMinePurchaseOrderCellState extends State<QGMinePurchaseOrderCell> {
  @override
  Widget build(BuildContext context) {
    var buyNum = widget.data?["finished_num"];
    var totalNum = widget.data?["total_num"];
    double progress = 0.0;

    if (totalNum > 0) {
      if (buyNum >= totalNum) {
        progress = 1.0;
      } else {
        progress = buyNum / totalNum;
      }
    }

    return Container(
      margin: EdgeInsets.only(top: 10.rpx, left: 10.rpx, right: 10.rpx),
      padding: EdgeInsets.all(10.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.rpx),
        color: Color(0xFFFFFFFF),
      ),
      child: Column(children: [
        Row(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(4.rpx),
              child: QGImage(
                  url: widget.data?["thumb_img"],
                  width: 82.rpx,
                  height: 82.rpx),
            ),
            SizedBox(width: 12.rpx),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: Text(
                          widget.data?["order_name"] ?? "",
                          style: TextStyle(
                              color: Color(0xFF111111),
                              fontSize: 14.rpx,
                              fontWeight: FontWeight.normal),
                        ),
                      ),
                      Text(
                        '￥${widget.data?["total_fee"]}',
                        style: TextStyle(
                            color: Color(0xFF111111),
                            fontSize: 14.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  SizedBox(height: 8.rpx),
                  widget.data?["status"] == 3
                      ? Text(
                          "卖出数量：${widget.data?["goods_num"]}",
                          style: TextStyle(
                              color: const Color(0xff999999),
                              fontSize: 12.rpx,
                              fontWeight: FontWeight.normal),
                        )
                      : Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              width: 140.rpx,
                              child: LinearProgressIndicator(
                                  backgroundColor: const Color(0xFFEAEAEA),
                                  color: const Color(0XFFFF3838),
                                  borderRadius: BorderRadius.circular(6.rpx),
                                  value: progress,
                                  minHeight: 5.rpx),
                            ),
                            SizedBox(height: 4.rpx),
                            Text(
                              "求购数量：$buyNum/$totalNum",
                              style: TextStyle(
                                  color: const Color(0xff999999),
                                  fontSize: 12.rpx,
                                  fontWeight: FontWeight.normal),
                            ),
                          ],
                        ),
                  SizedBox(height: 10.rpx),
                  Text(
                    widget.data?["created"] ?? "",
                    style: TextStyle(
                        color: const Color(0xff999999),
                        fontSize: 12.rpx,
                        fontWeight: FontWeight.normal),
                  ),
                ],
              ),
            ),
          ],
        ),
        SizedBox(height: 10.rpx),
        Container(height: 0.5.rpx, color: Color(0xFFEAEAEA)),
        SizedBox(height: 10.rpx),
        buildState(),
      ]),
    );
  }

  buildState() {
    Widget child = Container();

    switch (widget.data?["status"]) {
      case 0:
        child = buildWait(title: "求购中");
        break;
      case 1:
        child = buildSuccess(title: "转入成功");
        break;
      case 2:
        child = buildCancel();
        break;
      case 3:
        child = buildSuccess(title: "卖出成功");
        break;
      default:
        break;
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
            padding: EdgeInsets.symmetric(horizontal: 7.rpx, vertical: 3.rpx),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5.rpx),
              color: Color(0xFFF8F8F6),
            ),
            child: child),
        QGImage(asset: "mine_info_arrow"),
      ],
    );
  }

  buildWait({title}) {
    return Row(
      children: [
        QGImage(asset: "mine_order_waiting"),
        SizedBox(width: 10.rpx),
        Text(
          title,
          style: TextStyle(
              color: const Color(0xFFFF3838),
              fontSize: 11.rpx,
              fontWeight: FontWeight.normal),
        ),
      ],
    );
  }

  buildSuccess({title}) {
    return Row(
      children: [
        QGImage(asset: "mine_order_success"),
        SizedBox(width: 10.rpx),
        Text(
          title,
          style: TextStyle(
              color: const Color(0xFF00AF54),
              fontSize: 11.rpx,
              fontWeight: FontWeight.normal),
        ),
      ],
    );
  }

  buildCancel() {
    return Row(
      children: [
        QGImage(asset: "mine_order_cancel"),
        SizedBox(width: 10.rpx),
        Text(
          '订单已取消',
          style: TextStyle(
              color: const Color(0xff969696),
              fontSize: 11.rpx,
              fontWeight: FontWeight.normal),
        ),
      ],
    );
  }
}
